package com.example.yang.ministy;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * Created by Yang on 2016/9/21.
 */
public class loginActivity extends AppCompatActivity {
    private EditText accountet,passwdet;
    private CheckBox remenberpasswdcb;
    private TextView forgetaccounttv;
    private Button loginbtn;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_first);
        findView();
        initview();

    }
    public void findView(){
        accountet=(EditText)findViewById(R.id.account_et);
        passwdet=(EditText)findViewById(R.id.passwd_et);
        remenberpasswdcb=(CheckBox)findViewById(R.id.forgetpwd_cb);
        forgetaccounttv=(TextView)findViewById(R.id.remenberaccount_btn);
        loginbtn=(Button)findViewById(R.id.login_btn);

    }
    public void initview(){
        if (AppSharedPrefs.getInstance(this).getIsFirstRun()){
            startActivity(new Intent(this,GuideActivity.class));
        }
        final String account=AppSharedPrefs.getInstance(this).getAccount(null);
        final String passwd=AppSharedPrefs.getInstance(this).getAccount(null);
        if (account!=null&&passwd!=null){
            accountet.setText(account);
            passwdet.setText(account);
            remenberpasswdcb.setText(account);
        }
        forgetaccounttv.setOnClickListener(onClickListener);
        loginbtn.setOnClickListener(onClickListener);

    }
    private View.OnClickListener onClickListener=new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            switch (v.getId()) {
                case R.id.remenberaccount_btn:
                    startActivity(new Intent(loginActivity.this, basketball.class));
                    break;
                case R.id.login_btn:
                    final String account = accountet.getText().toString();
                    final String passwd = passwdet.getText().toString();
                    if (login(account, passwd)) {
                        if (remenberpasswdcb.isChecked()) saveaccountandpasswd(account, passwd);
                        startActivity(new Intent(loginActivity.this,MainFrameActivity.class));
                    }
                    break;
                default:
                    break;
            }
        }
    };

    private boolean login(String account, String passwd) {
        if (TextUtils.isEmpty(account)) {
            alertMessage(getResources().getString(R.string.message_account_error));
            return false;
        }
        if (TextUtils.isEmpty(passwd)) {
            alertMessage(getResources().getString(R.string.message_passwd_error));
            return false;
        }
        if (account.equals("meng")&&passwd.equals("qiu")) return true;
        else {
            alertMessage(getResources().getString(R.string.message_login_failed));
            return false;
        }
    }
    public void alertMessage(String message){
        Toast.makeText(this,message,Toast.LENGTH_SHORT).show();
    }
    private void saveaccountandpasswd(String account, String passwd) {
        AppSharedPrefs.getInstance(this).SetAccount(account);
        AppSharedPrefs.getInstance(this).SetPasswd(passwd);

    }

    




}
